<?php
	include_once("DAO.php");
        include_once(OBJECT_PATH . "/Component.php");
        include_once(OBJECT_PATH . "/Category.php");

	class ComponentDAO extends DAO
	{
		function addComponent($component)
		{
            $xmlRequest =   '<client action="add" object="component">' .
                            '<component>' .
                                '<id>-1</id>' .
                                '<name>' . $component->getName() . '</name>' .
                                '<description>' . $component->getDescription() . '</description>' .
                                '<price>' . $component->getPrice() . '</price>' .
                                '<stock>' . $component->getStock() . '</stock>' .
                                '<category>' . $component->getCategory() . '</category>' .
                            '</component>' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            foreach($xml[0]->attributes() as $attribute => $value)
            {
                if ($attribute == "object")
                    if ($value == "failed")
                        return false;
                    else
                        return true;
            }
		}

		function editComponent($component)
		{
            $xmlRequest =   '<client action="edit" object="component">' .
                            '<component>' .
                                '<id>' . $component->getId() . '</id>' .
                                '<name>' . $component->getName() . '</name>' .
                                '<description>' . $component->getDescription() . '</description>' .
                                '<price>' . $component->getPrice() . '</price>' .
                                '<stock>' . $component->getStock() . '</stock>' .
                                '<category>' . $component->getCategory() . '</category>' .
                            '</component>' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            foreach($xml[0]->attributes() as $attribute => $value)
            {
                if ($attribute == "object")
                    if ($value == "failed")
                        return false;
                    else
                        return true;
            }
		}

		function deleteComponent($componentId)
		{
            $xmlRequest =   '<client action="delete" object="component">' .
                            '<component><id>' . $componentId . '</id></component>' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            foreach($xml[0]->attributes() as $attribute => $value)
            {
                if ($attribute == "object")
                    if ($value == "failed")
                        return false;
                    else
                        return true;
            }
		}

		function getCategoryList()
		{
                    $xmlRequest =   '<client action="getall" object="category">' .
                                    '</client>';

                    // get the response and sent it to an xml object
                    $xmlResponse = $this->sendRequest($xmlRequest);
                    $xml = simplexml_load_string($xmlResponse);

                    $categories = new ArrayObject();
                    foreach ($xml->children() as $xmlCategory)
                    {

                        $category = new Category();
                        foreach($xmlCategory->children() as $categoryVar)
                        {
                            if ($categoryVar->getName() == "id")
                                $category->setId($categoryVar);
                            else if ($categoryVar->getName() == "name")
                                $category->setName($categoryVar);
                        }
                        $categories->append($category);
                    }

                    return $categories;
		}

        /*
         * A method that returns an array containing all of components
         * of a certain category.
         */
        function getComponentList($categoryId)
        {
            $xmlRequest =	'<client action="getphplist" object="component">' .
                            '<component><category>' . $categoryId . '</category></component>' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            $components = new ArrayObject();
            foreach ($xml->children() as $xmlComponent)
            {
                $component = new Component();
                foreach($xmlComponent->children() as $componentVar)
                {
                    if ($componentVar->getName() == "id")
                        $component->setId($componentVar);
                    else if ($componentVar->getName() == "name")
                        $component->setName($componentVar);
                }

                $components->append($component);
            }

            return $components;
        }


        /**
         * Returns a component containing all the component data of the
         * $id parameter
         */
        function getComponent($id)
        {
            $xmlRequest =   '<client action="get" object="component">' .
                            '<component><id>' . $id . '</id></component>' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            $component = new Component();
            foreach ($xml->children() as $xmlComponent)
            {
                foreach($xmlComponent->children() as $componentVar)
                {
                    if ($componentVar->getName() == "id")
                        $component->setId($componentVar);
                    else if ($componentVar->getName() == "name")
                        $component->setName($componentVar);
                    else if ($componentVar->getName() == "category")
                        $component->setCategory($componentVar);
                    else if ($componentVar->getName() == "description")
                        $component->setDescription($componentVar);
                    else if ($componentVar->getName() == "stock")
                        $component->setStock($componentVar);
                    else if ($componentVar->getName() == "price")
                        $component->setPrice($componentVar);
                }
                return $component;
            }

        }
    }
?>
